package dao;

import java.util.List;

import negocio.Mesa;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import hbt.HibernateUtil;

public class MesaDAO {

	private static MesaDAO instancia = null;
	private static SessionFactory sf = null;
	
	public MesaDAO()
	{
		
	}
	public static MesaDAO getInstancia()
	{
		if (instancia==null)
		{
			sf = HibernateUtil.getSessionFactory();
			instancia = new MesaDAO();
		}
		return instancia;
	}
	
	public void grabarMesa(Mesa m)
	{
		Session session = sf.openSession();
		session.beginTransaction();
		session.persist(m);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}
	
	public Mesa buscarMesa(int nroMesa) {
		Session session = sf.openSession();
		Mesa m = new Mesa();
		m.setNroMesa(nroMesa);
		session.load(m, m.getNroMesa());			
		session.close();
		return m;
	}
	public void eliminarMesa(int nroMesa) {
		Session session = sf.openSession();
		Mesa m = new Mesa();
		m.setNroMesa(nroMesa);
		session.delete(m);
		session.load(m, m.getNroMesa());			
		session.close();
		
	}
	@SuppressWarnings("unchecked")
	public List<Mesa> buscarMesas(){
		Session session = sf.openSession();
		List<Mesa> list = session.createQuery("from Mesa").list();
		session.close();
		return list;
	}
}
